package com.example.music;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.music.DB.UserInfo;

import org.litepal.LitePal;

import java.util.List;

public class myMessageActivity extends AppCompatActivity {
    private EditText uname ;
    private EditText uphone;
    private EditText uhometown ;
    private EditText uSignature;
    private EditText uemail ;
    private Button submit;
    public void init(){
        uname = findViewById(R.id.myName);
        uphone= findViewById(R.id.phone);
        uhometown = findViewById(R.id.hometown);
        uSignature = findViewById(R.id.signature);
        uemail = findViewById(R.id.email);
        submit = findViewById(R.id.submit);

        SharedPreferences sharedPreferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
        int curUserId = sharedPreferences.getInt("curUserId",-1);

        List<UserInfo> User = LitePal.where("Id=?",String.valueOf(curUserId))
                .find(UserInfo.class);

        uname.setText(User.get(0).getUsername());
        uphone.setText(User.get(0).getUserPhone());
        uhometown.setText(User.get(0).getHometown());
        uemail.setText(User.get(0).getEmail());
        uSignature.setText(User.get(0).getPersonSignature());

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(curUserId!=-1) {
                    new UserInfo(uname.getText().toString(),
                            uphone.getText().toString(),
                            uSignature.getText().toString(),
                            uhometown.getText().toString(),
                            uemail.getText().toString()).updateAll("id = ?", String.valueOf(curUserId));
                    showToast("修改成功！");
                }
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_home);
        init();
    }
    public void showToast(String msg){
        // 创建一个Toast消息
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}